<template>
    <div>
        <RadioGroup v-model="radio">
            <Radio :label="0">
                <span>文字模式</span>
            </Radio>
            <Radio :label="1">
                <span>接口调用模式</span>
            </Radio>
        </RadioGroup>
        <div v-if="radio == 0">
            <div>
                赋值前缀:
                <Input v-model="formatTopPrefix" placeholder="Enter something..." style="width: 200px" />

            </div>
            <div>
                赋值后缀
                <Input v-model="formatEndPrefix" placeholder="Enter something..." style="width: 200px" />

            </div>
            <div>保留小数位
                <InputNumber :max="10" :min="1" v-model="NumberReserve" />
            </div>
            <div style="margin-top: 20px;">
                输入文本框
                <br />
                <Input v-model="inputVal" maxlength="1000" show-word-limit type="textarea" placeholder="Enter something..."
                    style="width: 600px" />
                <br />
                <Button @click="Generate(inputVal, formatTopPrefix, formatEndPrefix, NumberReserve)">生成</Button>
            </div>
            <div style="margin-top: 20px;">
                formData 定义数据
                <div>
                    {{ formData }}
                </div>
            </div>
            <div style="margin-top: 20px;">
                methods 赋值数据
                <div v-html="methodsData">
                </div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            radio: 0,
            formData: "",
            methodsData: "",
            inputVal: "",
            formatTopPrefix: "this.data",
            formatEndPrefix: "res.data",
            NumberReserve: 0,
        }
    },
    methods: {
        Generate(data, formatTopPrefix, formatEndPrefix, NumberReserve = 0) {
            data = data.split(/[(\r\n)\r\n]+/);
            let text = "";
            let dataObj = {};
            data.forEach(ele => {
                if (ele != "") {
                    let arr = ele.split(":")
                    // 生成data内对象
                    dataObj[arr[0]] = isNaN(Number(arr[1])) ? "" : 0;
                    if (NumberReserve > 0) {
                        if (!isNaN(Number(arr[1]))) {
                            text += `${formatTopPrefix}.${arr[0]} = Number(${formatEndPrefix}.${arr[0]}).toFixed(${NumberReserve});<br/>
`
                        } else {
                            text += `${formatTopPrefix}.${arr[0]} = ${formatEndPrefix}.${arr[0]};<br/>
`
                        }

                    } else {
                        text += `${formatTopPrefix}.${arr[0]} = ${formatEndPrefix}.${arr[0]};<br/>
`
                    }

                }

            })
            console.log("生成data定义值中.......", dataObj);
            this.formData = dataObj
            console.log("-------------------------------------------")
            console.log("生成methods定义值中.......")
            this.methodsData = text
        }
    },
    mounted() {

    },
}
</script>
<style></style>